// pages/user/invoice/invoice.js
import orderIndex  from '../../../api/order';
import userIndex  from '../../../api/user';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    orderNo:'',
    amount:'',
    invoiceContent:'',
    invoiceType:'',

    email:'',
    idNum:'',
    title:'',
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    
    this.setData({
      orderNo:options.orderNo,
      // email:invoice.email?invoice.email:'',
      // idNum:invoice.idNum?invoice.idNum:'',
      // title:invoice.title?invoice.title:'',
    })
    this.getInvoiceInfo()
    this.getinvoiceDefualt()
  },
  getinvoiceDefualt(){
    userIndex.apiUserinvoiceDefualt().then(res=>{
      this.setData({
        email:res.data?res.data.email:'',
        idNum:res.data?res.data.idNum:'',
        title:res.data?res.data.title:'',
      })
    })
  },
  getInvoiceInfo(){
    orderIndex.apiInvoiceInfo({orderNo:this.data.orderNo}).then(res=>{
      this.setData({
        orderNo:res.data.orderNo,
        amount:res.data.amount,
        invoiceContent:res.data.invoiceContent,
        invoiceType:res.data.invoiceType,
      })
    })
  },
  getInput(e){    
    var name=e.currentTarget.dataset.name
    var value=e.detail.value
    this.setData({
      [name]:value
    })
  },
  getInvoice(){
    wx.navigateTo({
      url: `/pages/user/invoiceManage/invoiceManage`,
    })
  },
  getSumbit(){
    if(!this.data.title){
      wx.showToast({
        title:'请填写发票抬头',
          icon: 'none',
          duration:3000,
        }) 
        return false
    }
    if(!this.data.idNum){
      wx.showToast({
        title:'请填写纳税人识别号',
          icon: 'none',
          duration:3000,
        }) 
        return false
    }
    if(!this.data.email){
      wx.showToast({
        title:'请填写邮箱地址',
          icon: 'none',
          duration:3000,
        }) 
        return false
    }
    var opt={
      email:this.data.email,
      idNum:this.data.idNum,
      title:this.data.title,
      orderNo:this.data.orderNo
    }
   wx.requestSubscribeMessage({
    tmplIds: ['TCteIluoaa3uTDgpQt1KX3xun1sYXef82jXTZFTidSw'],
    complete (complete) {
      orderIndex.apiApplyInvoice(opt).then(res=>{
        wx.showToast({
          title:'开票成功',
            icon: 'none',
            duration:3000,
          }) 
      })
    }
  })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})